<?php
/**
* 名称：种类模型类。
* 描述：种类操作的相关程式。
*/
final class Model_Common_Sort extends Model{
    /**
    * 描述：获取类别名称。
    * @static
    * @access public
    * @param  int    $class 必选 类别标识号。
    * @return array
    */
    public static function GetClassesName($table,$class){
        $result1=self::$Database->GetRow(
            'SELECT name,parent FROM '.self::$Database->Table($table).' WHERE id=?',
            array($class)
        );
        $name=$result1['name'];
        //~
        if((int)$result1['parent']>0){
            $result2=self::$Database->GetRow(
                'SELECT name,parent FROM '.self::$Database->Table($table).' WHERE id=?',
                array($result1['parent'])
            );
            $name=($result2['name'].'/').$name;
        }
        //~
        if(isset($result2)&&(int)$result2['parent']>0){
            $result3=self::$Database->GetRow(
                'SELECT name,parent FROM '.self::$Database->Table($table).' WHERE id=?',
                array($result2['parent'])
            );
            $name=($result3['name'].'/').$name;
        }
        //~
        return $name;
    }

    /**
    * 描述：获取全部类别数据。
    * @static
    * @access public
    * @return array
    */
    public static function GetClassData($table){
        $data=self::$Database->GetAll(
            'SELECT * FROM '.self::$Database->Table($table).' WHERE parent is NULL ORDER BY id ASC'
        );
        //~
        foreach($data as &$item){
            $item['data']=self::$Database->GetAll(
                'SELECT * FROM '.self::$Database->Table($table).' WHERE parent=? ORDER BY id ASC',
                array($item['id'])
            );
            foreach($item['data'] as &$subItem){
                $subItem['data']=self::$Database->GetAll(
                    'SELECT * FROM '.self::$Database->Table($table).' WHERE parent=? ORDER BY id ASC',
                    array($subItem['id'])
                );
            }
        }
        return $data;
    }

    /**
    * 描述：获取类别的子类数据。
    * @static
    * @access public
    * @return array
    */
    public static function GetSubclassData($table,$class=NULL){
        if((int)$class>0){
            return self::$Database->GetAll(
                'SELECT id,name FROM '.self::$Database->Table($table).' WHERE parent=? ORDER BY id ASC',
                array($class)
            );
        }
        else{
            return NULL;
        }
    }
}